<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\Request;
use app\api\model\Test as TestModel;
use app\api\model\TestM as TestModelM;
use app\api\model\TestC as TestModelC;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Test extends Base
{

    public function inserExcel(Request $request){

        $path = public_path().'/1.xlsx';
        $reader = PHPExcel_IOFactory::createReader('Excel2007');
        $excel = $reader->load($path,$encode = 'utf-8');
        $sheet = $excel->getSheet(0)->toArray();

        array_shift($sheet);  //删除第一个数组(标题);
        $data = [];
        $i = 0;
        foreach ($sheet as $k => $v) {
            $data[$k]['name'] = $v[0];
            $data[$k]['accout'] = $v[1];
            $data[$k]['openid'] = $v[2];
            $data[$k]['jobname'] = $v[3];
            $data[$k]['depart'] = $v[4];
            $i++;
        }
        $data = $data;
        $TestModel = new TestModel;
        $res = $TestModel->insertAll($data);
        return $TestModel->getLastSql();

    }

    public function inserExcelM(Request $request){

        $path = public_path().'/oa.xlsx';
        $reader = PHPExcel_IOFactory::createReader('Excel2007');
        $excel = $reader->load($path,$encode = 'utf-8');
        $sheet = $excel->getSheet(0)->toArray();

        array_shift($sheet);  //删除第一个数组(标题);
        $data = [];
        $i = 0;
        foreach ($sheet as $k => $v) {
            $data[$k]['oa_id'] = $v[0];
            $data[$k]['oa_accout'] = $v[1];
            $data[$k]['oa_name'] = $v[2];
            $i++;
        }
        $data = $data;
        $TestModel = new TestModelM;
        $res = $TestModel->insertAll($data);
        return $TestModel->getLastSql();

    }

    public function inserExcelC(Request $request){

        $path = public_path().'/c.xlsx';
        $reader = PHPExcel_IOFactory::createReader('Excel2007');
        $excel = $reader->load($path,$encode = 'utf-8');
        $sheet = $excel->getSheet(0)->toArray();

        array_shift($sheet);  //删除第一个数组(标题);
        $data = [];
        $i = 0;
        foreach ($sheet as $k => $v) {
            $data[$k]['empuid'] = $v[0];
            $data[$k]['c_mobile'] = $v[1];
            $data[$k]['c_name'] = $v[2];
            $data[$k]['c_jobname'] = $v[3];
            $data[$k]['c_depart'] = $v[4];
            $data[$k]['c_openid'] = $v[5];
            $i++;
        }
        $data = $data;
        $TestModel = new TestModelC;
        $res = $TestModel->insertAll($data);
        return $TestModel->getLastSql();

    }

    public function finddata(){

        $TestModel = new TestModelM;
        $list = $TestModel->alias('m')
                          ->Join('test t','m.oa_accout = t.accout')
                          ->Join('test_c c','t.openid = c.empuid')
                          ->select()->toArray();
        //return $TestModel->getLastSql();
        //return json($list);
        $file_name = "用户列表导出";
        $arr = array(
            array('oa_id','1'),
            array('oa_accout','2'),
            array('oa_name','3'),
            array('id','4'),
            array('c_openid','5'),
            array('accout','6'),
            array('empuid','7'),
            array('jobname','8'),
            array('depart','9'),
            array('c_mobile','10')
        );
        $str = "导出时间:".date('Y-m-d H:i:s',time());
        $this->getExceltest($file_name,$arr,$list,$str);
    }

    //导出
    private function getExceltest($expTitle, $expCellName, $expTableData, $str = "")
    {
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $expTitle . date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        $common = $str;
        $objPHPExcel = new Spreadsheet();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $common);
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '2', $expCellName[$i][1]);
        }
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 3), ' ' . $expTableData[$i][$expCellName[$j][0]]);

            }
        }
        foreach ($cellName as $value) {
            $objPHPExcel->getActiveSheet()->getColumnDimension($value)->setWidth(20);

        }
        ob_clean();
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $expTitle . '.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = IOFactory::createWriter($objPHPExcel, 'Xlsx');
        $objWriter->save('php://output');
        $objPHPExcel->disconnectWorksheets();
        unset($objPHPExcel);
        exit();
    }

}
